package admin.controllers;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

import models.AdsDealModel;
import models.DealModel;
import admin.data.AdsDealData;

@SuppressWarnings("serial")
public class AdvertisingManageServlet extends HttpServlet {

	private static final Logger log = Logger
			.getLogger(AdvertisingManageServlet.class.getName());

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		try {
			String[] splitted = request.getRequestURI().split("/");
			if (splitted.length == 4) {
				String action = splitted[3];
				if (action.equals("right")) {
					request.setAttribute("url", request.getRequestURL()
							.toString());
					AdsDealModel adSpecial = AdsDealData.getByType("special");
					List<DealModel> homeAdvertising = AdsDealData
							.getListDealByType("special");
					List<DealModel> listRight = AdsDealData
							.getListDealByType("right");
					List<DealModel> listTop = AdsDealData
							.getListDealByType("top");
					request.setAttribute("adSpecial", adSpecial);
					request.setAttribute("homeAdvertising", homeAdvertising);
					request.setAttribute("listMenuRight", listRight);
					request.setAttribute("listMenuTop", listTop);
					request.getRequestDispatcher(
							"/admin/pages/advertising_right_manager.jsp")
							.forward(request, response);
				} else {

				}
			}
		} catch (Exception e) {
			System.out.println("Error when get list Deal Advertising !");
			log.warning("Error when get list Deal Advertising !");
			log.warning(e.toString());
			e.printStackTrace();
		}
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		try {
			AdsDealModel adSpecial = AdsDealData.getByType("special");
			BlobstoreService blobstoreService = BlobstoreServiceFactory
					.getBlobstoreService();
			Map<String, List<BlobKey>> blobs = blobstoreService
					.getUploads(request);
			List<BlobKey> blobKeys = blobs.get("specialImages");
			List<String> listImg = new ArrayList<String>();
			if (blobKeys != null) {
				String img = "";
				for (BlobKey blobKey : blobKeys) {
					byte[] arr = blobstoreService.fetchData(blobKey, 0, 20);
					if (arr.length > 0) {
						img = blobKey.getKeyString();
					}
					if (img != null && img.length() > 0) {
						listImg.add(img);						
					}
				}

			}
			adSpecial.setImages(listImg);
			AdsDealData.insert(adSpecial);
			HttpSession session = request.getSession();
			session.setAttribute("success", "Thao tác thành công !");
			response.sendRedirect("/admin/ads/right");
		} catch (Exception e) {
			System.out.println("Error when post to Deal Advertising !");
			log.warning("Error when post to Deal Advertising !");
			log.warning(e.toString());
			e.printStackTrace();
		}
	}
}
